home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / publish.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  7.1 KB  |  228 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 2001-2002
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the NPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the NPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. var gPublishHandler = null;
  39.  
  40. function EditorPublish(destinationDirectoryLocation, fileName, login, password)
  41. {
  42.   post_current_editor_contents_to_server(destinationDirectoryLocation, fileName, login, password);
  43. //  PublishCopyFile(srcDirectoryLocation, destinationDirectoryLocation, fileName);
  44. }
  45.  
  46. var gPublishIOService;
  47. function GetIOService()
  48. {
  49.   if (gPublishIOService)
  50.     return gPublishIOService;
  51.  
  52.   gPublishIOService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  53.   if (!gPublishIOService)
  54.     dump("failed to get io service\n");
  55.   
  56.   return gPublishIOService;
  57. }
  58.  
  59. // this function takes a login and password and adds them to the destination url
  60. function get_destination_channel(destinationDirectoryLocation, fileName, login, password)
  61. {
  62.   try
  63.   {
  64.     var ioService = GetIOService();
  65.     if (!ioService)
  66.       return;
  67.  
  68.     // create a channel for the destination location
  69.     var fullurl = destinationDirectoryLocation + fileName;
  70.     dump("location is "+fullurl+"\n");
  71.     destChannel = create_channel_from_url(ioService, fullurl, login, password);
  72.     if (!destChannel)
  73.     {
  74.       dump("can't create dest channel\n");
  75.       return;
  76.     }
  77.     try {
  78.     dump("about to set callbacks\n");
  79.     destChannel.notificationCallbacks = window.docshell;  // docshell
  80.     dump("notification callbacks set\n");
  81.     }
  82.     catch(e) {dump(e+"\n");}
  83.  
  84.     var ftpChannel = destChannel.QueryInterface(Components.interfaces.nsIFTPChannel);
  85.     if (ftpChannel) dump("ftp channel found\n");
  86.     if (ftpChannel)
  87.       return ftpChannel;
  88.     var httpChannel = destChannel.QueryInterface(Components.interfaces.nsIHTTPChannel);
  89.     if (httpChannel) dump("http channel found\n");
  90.     if (httpChannel)
  91.       return httpChannel;
  92.     var httpsChannel = destChannel.QueryInterface(Components.interfaces.nsIHTTPSChannel);
  93.     if (httpsChannel) dump("https channel found\n");
  94.     if (httpsChannel)
  95.       return httpsChannel;
  96.     else
  97.       return null;
  98.   }
  99.   catch (e)
  100.   {
  101.     return null;
  102.   }
  103. }
  104.  
  105. function output_current_editor_to_channel(aChannel)
  106. {
  107.   var formatType = 'text/' + editorShell.editorType;
  108.   var flags = 256; // nsIDocumentEncoder::OutputEncodeEntities
  109.   var charset = editorShell.GetDocumentCharacterSet();
  110.   editorShell.editor.outputToStream(aChannel, formatType, charset, flags); 
  111.  //   protocolChannel.uploadStream = contentsStream;
  112. }
  113.  
  114. function post_current_editor_contents_to_server(newLocation, fileName, login, password)
  115. {
  116.   try
  117.   {
  118.     var protocolChannel = get_destination_channel(newLocation, fileName, login, password);
  119.     if (!protocolChannel)
  120.     {
  121.       dump("failed to get a destination channel\n");
  122.       return;
  123.     }
  124.  
  125.     output_current_editor_to_channel(protocolChannel);
  126.     protocolChannel.asyncOpen(gPublishingListener, null);
  127.     dump("done\n");
  128.   }
  129.   catch (e)
  130.   {
  131.     dump("an error occurred: " + e + "\n");
  132.   }
  133. }
  134.  
  135. // this function takes a full url, creates an nsIURI, and then creates a channel from that nsIURI
  136. function create_channel_from_url(ioService, aURL, aLogin, aPassword)
  137. {
  138.   try
  139.   {
  140.     var nsiuri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI);
  141.     if (!nsiuri)
  142.       return null;
  143.     nsiuri.spec = aURL;
  144.     if (aLogin)
  145.     {
  146.       nsiuri.username = aLogin;
  147.       if (aPassword)
  148.         nsiuri.password = aPassword;
  149.     }
  150.     return ioService.newChannelFromURI(nsiuri);
  151.   }
  152.   catch (e) 
  153.   {
  154.     dump(e+"\n");
  155.     return null;
  156.   }
  157. }
  158.  
  159. function PublishCopyFile(srcDirectoryLocation, destinationDirectoryLocation, fileName, aLogin, aPassword)
  160. {
  161.   // append '/' if it's not there; inputs should be directories so should end in '/'
  162.   if ( srcDirectoryLocation.charAt(srcDirectoryLocation.length-1) != '/' )
  163.     srcDirectoryLocation = srcDirectoryLocation + '/';
  164.   if ( destinationDirectoryLocation.charAt(destinationDirectoryLocation.length-1) != '/' )
  165.     destinationDirectoryLocation = destinationDirectoryLocation + '/';
  166.  
  167.   try
  168.   {
  169.     // grab an io service
  170.     var ioService = GetIOService();
  171.     if (!ioService)
  172.       return;
  173.  
  174.     // create a channel for the source location
  175.     srcChannel = create_channel_from_url(ioService, srcDirectoryLocation + fileName, null, null);
  176.     if (!srcChannel)
  177.     {
  178.       dump("can't create src channel\n");
  179.       return;
  180.     }
  181.  
  182.     // create a channel for the destination location
  183.     var ftpChannel = get_destination_channel(destinationDirectoryLocation, fileName, aLogin, aPassword);
  184.     if (!ftpChannel)
  185.     {
  186.       dump("failed to get ftp channel\n");
  187.       return;
  188.     }
  189.     ftpChannel.uploadStream = srcChannel.open();
  190.     ftpChannel.asyncOpen(null, null);
  191.     dump("done\n");
  192.   }
  193.   catch (e)
  194.   {
  195.     dump("an error occurred: " + e + "\n");
  196.   }
  197. }
  198.  
  199. var gPublishingListener =
  200. {
  201.   QueryInterface: function(aIId, instance)
  202.   {
  203.     if (aIId.equals(Components.interfaces.nsIStreamListener))
  204.       return this;
  205.     if (aIId.equals(Components.interfaces.nsISupports))
  206.       return this;
  207.     
  208.     dump("QueryInterface " + aIId + "\n");
  209.     throw Components.results.NS_NOINTERFACE;
  210.   },
  211.  
  212.   onStartRequest: function(request, ctxt)
  213.   {
  214.     dump("onStartRequest status = " + request.status + "\n");
  215.   },
  216.  
  217.   onStopRequest: function(request, ctxt, status, errorMsg)
  218.   {
  219.     dump("onStopRequest status = " + request.status + " " + errorMsg + "\n");
  220.   },
  221.  
  222.   onDataAvailable: function(request, ctxt, inStream, sourceOffset, count)
  223.   {
  224.     dump("onDataAvailable status = " + request.status + " " + count + "\n");
  225.   }
  226. }
  227.  
  228.